package com.tale.springbootatm.dao;

import com.tale.springbootatm.entity.User;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;
import java.util.Date;

/**
 * author: 蔡奇峰
 * date: 2020/4/11 14:26
 * @Version V1.0
 **/
@Repository
public interface UserRepository extends JpaRepository<User, Long> {

    /**
     * 登录
     * @param bankCard
     * @param password
     * @return
     */
    User findByBankCardAndPassword(long bankCard, String password);

    /**
     * 查询（通过卡号）
     * @param bankCard
     * @return
     */
    User findByBankCard(long bankCard);

    /**
     * 更新登录时间
     * @param bankCard
     * @param loginDate
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    @Modifying
    @Query("update User u set u.loginDate = ?2 where u.bankCard = ?1")
    int updateLoginDate(@Param("bankCard")long bankCard, @Param("loginDate")Date loginDate);
//    @Query("update User u set u.loginDate = :loginDate where u.bankCard = :bnakCard")

    /**
     * 更新账户余额
     * @param bankCard
     * @param balance
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    @Modifying
    @Query("update User u set u.balance = ?2 where u.bankCard = ?1")
    int updateBalance(@Param("bankCard")long bankCard, @Param("balance")double balance);

    @Transactional(rollbackFor = Exception.class)
    @Modifying
    @Query("update User u set u.password = ?2 where u.bankCard = ?1")
    int updatePassword(@Param("bankCard")long bankCard, @Param("password")String password);


    /**
     * 更新卡号状态
     * @param bankCard
     * @param type
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    @Modifying
    @Query("update User u set u.uType = ?2 where u.bankCard = ?1")
    int updateType(@Param("bankCard")long bankCard, @Param("type")int type);
}
